#include <stdio.h>
#include <stdlib.h>

#include "od_framework.h"
#include "od_sip_register.h"
#include "od_sip_event_distributor.h"
#include "od_sip_invite.h"
#include "od_sip_handle_event.h"
#include "od_sip_state_machine.h"
#include "od_sip_other_event.h"
#include "od_sip_register_server.h"
#include "usecase_sip.h"
#include "sip_param.h"


static gctl_obj g_obj;

static void *sip_register_static_para_create(void *usr, const char *task_name)
{
    od_sip_register_static_params *s_param;

    s_param = (od_sip_register_static_params *)od_malloc(sizeof(od_sip_register_static_params));
    OD_SIP_ASSERT_RETURN(s_param, NULL);

    s_param->task_name = task_name;
    s_param->node = REG_NODE_1;

    return (void *)s_param;
}

static void *sip_register_dynamic_init(void *usr)
{
    od_sip_register_dynamic_params *d_param;

    d_param = (od_sip_register_dynamic_params *)od_malloc(sizeof(od_sip_register_dynamic_params));
    OD_SIP_ASSERT_RETURN(d_param, NULL);

    strcpy(d_param->username, "34020000001180000001");
    strcpy(d_param->passwd, "12345678");
    strcpy(d_param->proxy_addr, "10.1.41.63");
    d_param->proxy_port = 5060;
    d_param->expires = 300;
    d_param->reg_status = REGISTER_INIT_STATUS;
    strcpy(d_param->proxy_id, "34020000002000000001");
    strcpy(d_param->proxy_domain, "3402000000");

    return (void *)d_param;
}

static void *sip_register_static_para_create2(void *usr, const char *task_name)
{
    od_sip_register_static_params *s_param;

    s_param = (od_sip_register_static_params *)od_malloc(sizeof(od_sip_register_static_params));
    OD_SIP_ASSERT_RETURN(s_param, NULL);

    s_param->task_name = task_name;
    s_param->node = REG_NODE_2;

    return (void *)s_param;
}

static void *sip_register_dynamic_init2(void *usr)
{
    od_sip_register_dynamic_params *d_param;

    d_param = (od_sip_register_dynamic_params *)od_malloc(sizeof(od_sip_register_dynamic_params));
    OD_SIP_ASSERT_RETURN(d_param, NULL);

    strcpy(d_param->username, "34020000001180000001");
    strcpy(d_param->passwd, "12345678");
    strcpy(d_param->proxy_addr, "10.1.41.63");
    d_param->proxy_port = 5060;
    d_param->expires = 60;
    d_param->reg_status = REGISTER_INIT_STATUS;
    strcpy(d_param->proxy_id, "34020000002000000001");
    strcpy(d_param->proxy_domain, "3402000000");

    return (void *)d_param;
}


static void *sip_event_distributor_para_create(void *usr, const char *task_name)
{
    od_sip_event_distributor_static_params *s_param;

    s_param = (od_sip_event_distributor_static_params *)od_malloc(sizeof(od_sip_event_distributor_static_params));
    OD_SIP_ASSERT_RETURN(s_param, NULL);

    s_param->task_name = task_name;
    return (void *)s_param;
}

static void *sip_invite_static_para_create(void *usr, const char *task_name)
{
    od_sip_invite_static_params *s_param;

    s_param = (od_sip_invite_static_params *)od_malloc(sizeof(od_sip_invite_static_params));
    OD_SIP_ASSERT_RETURN(s_param, NULL);

    s_param->task_name = task_name;
    s_param->h_state_machine = &g_obj.h_sip_state_machine;

    return (void *)s_param;
}

static void *sip_invite_dynamic_init(void *usr)
{
    od_sip_invite_dynamic_params *d_param;

    d_param = (od_sip_invite_dynamic_params *)od_malloc(sizeof(od_sip_invite_dynamic_params));
    OD_SIP_ASSERT_RETURN(d_param, NULL);

    strcpy(d_param->dst_uri, "260");

    return (void *)d_param;
}

static void *sip_handle_event_static_para_create(void *usr, const char *task_name)
{
    od_sip_handle_event_static_params *s_param;

    s_param = (od_sip_handle_event_static_params *)od_malloc(sizeof(od_sip_handle_event_static_params));
    OD_SIP_ASSERT_RETURN(s_param, NULL);

    s_param->task_name = task_name;
    s_param->h_state_machine = &g_obj.h_sip_state_machine;

    return (void *)s_param;
}


static void *sip_state_machine_static_para_create(void *usr, const char *task_name)
{
    od_sip_state_machine_static_params *s_param;

    s_param = (od_sip_state_machine_static_params *)od_malloc(sizeof(od_sip_state_machine_static_params));
    OD_SIP_ASSERT_RETURN(s_param, NULL);

    s_param->task_name = task_name;
    return (void *)s_param;
}

static void *sip_other_event_static_para_create(void *usr, const char *task_name)
{
    od_sip_other_event_static_params *s_param;

    s_param = (od_sip_other_event_static_params *)od_malloc(sizeof(od_sip_other_event_static_params));
    OD_SIP_ASSERT_RETURN(s_param, NULL);

    s_param->task_name = task_name;
    return (void *)s_param;
}

static void *sip_reg_server_static_para_create(void *usr, const char *task_name)
{
    od_sip_reg_server_static_params *s_param;

    s_param = (od_sip_reg_server_static_params *)od_malloc(sizeof(od_sip_reg_server_static_params));
    OD_SIP_ASSERT_RETURN(s_param, NULL);

    s_param->task_name = task_name;

    return (void *)s_param;
}

static void *sip_reg_server_dynamic_init(void *usr)
{
    od_sip_reg_server_dynamic_params *d_param;

    d_param = (od_sip_reg_server_dynamic_params *)od_malloc(sizeof(od_sip_reg_server_dynamic_params));
    OD_SIP_ASSERT_RETURN(d_param, NULL);

    strcpy(d_param->userID, "34020000002220000001");
    strcpy(d_param->passwd, "12345678");
    strcpy(d_param->realm, "3402000000");
    d_param->algorithm = 5;
    strcpy(d_param->nonce, "12345678");
    d_param->auth_flg = OD_FALSE;

    return (void *)d_param;
}



static const app_task_spawn_info_t sip_app_list[] = 
{
    {TSK_NAME_SIP_REIGSTER, 87,
        (void *)sip_register_static_para_create, 
        (void *)sip_register_dynamic_init, 
        (void *)od_sip_register_create
    },

    {TSK_NAME_SIP_REIGSTER2, 87,
        (void *)sip_register_static_para_create2, 
        (void *)sip_register_dynamic_init2, 
        (void *)od_sip_register_create
    },
    
    {TSK_NAME_SIP_EVT_DSTRB, 87,
        (void *)sip_event_distributor_para_create, 
         NULL, 
        (void *)od_sip_event_distributor_create
    }, 

    {TSK_NAME_SIP_INVITE, 87,
        (void *)sip_invite_static_para_create, 
        (void *)sip_invite_dynamic_init, 
        (void *)od_sip_invite_create
    },

    {TSK_NAME_SIP_HANDLE_EVENT, 87,
        (void *)sip_handle_event_static_para_create, 
         NULL, 
        (void *)od_sip_handle_event_create
    },

    {TSK_NAME_SIP_STATE_MACHINE, 87,
        (void *)sip_state_machine_static_para_create, 
        NULL,
        (void *)od_sip_state_machine_create
    },

    {TSK_NAME_SIP_OTHER_EVENT, 87,
        (void *)sip_other_event_static_para_create, 
        NULL,
        (void *)od_sip_other_event_create
    },

    {TSK_NAME_SIP_REG_SERVER, 87,
        (void *)sip_reg_server_static_para_create, 
        (void *)sip_reg_server_dynamic_init, 
        (void *)od_sip_reg_server_create
    },
};

static task_name_t  sip_starting_list[] =
{
    {TSK_NAME_SIP_EVT_DSTRB},
    //{TSK_NAME_SIP_REIGSTER},
    //{TSK_NAME_SIP_REIGSTER2},
    //{TSK_NAME_SIP_INVITE},
    //{TSK_NAME_SIP_HANDLE_EVENT},
    {TSK_NAME_SIP_OTHER_EVENT},
    {TSK_NAME_SIP_REG_SERVER},
    {TSK_NAME_SIP_STATE_MACHINE}
};


/*用于函数句柄的初始化*/
static void sip_task_handle_init(od_framework_t * framework)
{
    g_obj.h_sip_state_machine = ghash_find(framework->task_table, TSK_NAME_SIP_STATE_MACHINE);
}


int32_t sip_module_start(od_framework_t * framework)
{
    int ret;
    
    ret = od_framework_load(framework, 
                            sip_app_list, 
                            sip_starting_list, 
                            PJ_ARRAY_SIZE(sip_starting_list));
    if (ret < 0)
        RPTERR("od framework load error!!!");

    sip_task_handle_init(framework);
    
    ret = od_framework_start(framework, 
                            sip_app_list, 
                            sip_starting_list, 
                            PJ_ARRAY_SIZE(sip_starting_list));
    if (ret < 0)
        RPTERR("od framework start error!!!");
                            
    return ret;
}

int32_t sip_module_stop(od_framework_t * framework)
{
    return od_framework_stop(framework,
                             sip_starting_list,
                             PJ_ARRAY_SIZE(sip_starting_list));
}


/*_*/

